
//服务器
// let urlConfig="https://test2.cqhmx.cn/api"; //测试服
// let urlConfig="http://cqzeyou.test/api"; //陈静
let urlConfig = "https://www.cqzeyou.com/api";//外网正式环境
let Platform='APP';

const interceptor = (response) => {
	if (response.statusCode < 200 || response.statusCode >= 300) {
		uni.showToast({
				title: response.data.message ? response.data.message :response.statusCode==401?'身份验证失败':'未知错误',
				duration: 3000,
				icon: 'none'
		})
		if(response.statusCode==401){
			uni.removeStorageSync("token");
			uni.removeStorageSync("userInfo");
			uni.showToast({
				title: '请稍候重试！',
				icon: 'none',
				complete: () => {
					uni.navigateTo({
						 url: '/pagesA/pages/login/login'
					})
				}
			});
		}else if(response.statusCode==421){
			setTimeout(() => {
				uni.navigateBack({
					delta: 1
				});
			}, 1000)
		}
		return false;
	}
	return true;
}

const request = {
	urlConfig,
	Platform,
    get(url, data = {}) {
        return new Promise((resolve, reject) => {
					console.log(data)
            uni.request({
            	url: urlConfig + url,
							data: data.data,
							header:{
								'Content-Type': 'application/json',
								'Authorization':uni.getStorageSync('token')?('Bearer '+uni.getStorageSync('token').access_token):null,
								// 'X-Client-Hash':uni.getStorageSync('client_hash')|| null,
								'Platform':Platform,
								'Accept':'application/json',
								'City':uni.getStorageSync('LocationName')?uni.getStorageSync('LocationName').city:null,
								'Client-Source':'LOGISTIC'
							},
            	success: (response) => {
					!interceptor(response) ? reject(response) : resolve(response.data)
            	},
            	fail: (error) => {
            		reject(error);
            	}
            })
        })
    },
    post(url, data = {}) {
			return new Promise((resolve, reject) => {
				console.log(data)
				uni.request({
					url: urlConfig + url,
					data: data.data,
					method: 'POST',
					header:{
						'Content-Type': 'application/json',
						'Authorization':uni.getStorageSync('token')?('Bearer '+uni.getStorageSync('token').access_token):null,
						// 'X-Client-Hash':uni.getStorageSync('client_hash')|| null,
						'Platform':Platform,
						'Accept':'application/json',
						'City':uni.getStorageSync('LocationName')?uni.getStorageSync('LocationName').city:null,
						'Client-Source':'LOGISTIC'
					},
					success: (response) => {
						!interceptor(response) ? reject(response) : resolve(response.data)
					},
					fail: (error) => {
						reject(error);
					}
				})
			})
    },
		postFile(url, data = {}) {
			return new Promise((resolve, reject) => {
				uni.uploadFile({
					url: urlConfig + url,
					name: 'image',
					filePath: data.data.imgs,
					header:{
						'Authorization':uni.getStorageSync('token')?('Bearer '+uni.getStorageSync('token').access_token):null,
						// 'X-Client-Hash':uni.getStorageSync('client_hash')|| null,
						'Platform':Platform,
						'Accept':'application/json',
						'Client-Source':'LOGISTIC'
					},
					formData:{save_dir:data.data.save_dir},
					success: (response) => {
						!interceptor(response) ? reject(response) : resolve(JSON.parse(response.data))
					},
					fail: (error) => {
						reject(error);
					}
				})
			})
		},
}

//拦截器
uni.addInterceptor('request', {
	//请求前
  invoke(args) {
  },
	//请求后
  success(response) {

  }, 
})

// 暴露出去
export default request